<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

require_once(BASEPATH . "../hybridauth/hybridauth.php");

class Users_model extends CI_Model {

	private $hybridauth;
	
    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
        $this->hybridauth = new Hybrid_Auth();
    }
    
    function get_all()
    {
        $query = $this->db->get('users');
        return $query->result();
    }

    function insert($data)
    {
    	$this->db->insert('users', $data);
        return $this->db->insert_id() ;
    }

    function update($id, $data)
    {
        return $this->db->update('users', $data, array('id' => $id));
    }
    
    function remove($id)
    {
        return $this->db->delete('users', array('id' => $id));
    }
    
    function login_with_google()
	{

      	if( $this->hybridauth->hasError() ) return false;

      	if( $this->hybridauth->hasSession() )	return true;
      
  		$params                         = array(); 
  		// URL of your application which will be called after authentication 
  
  		$params["hauth_return_to"]      = $this->hybridauth->getCurrentUrl() ; // after authentication, return to this same page
  		$provider                       = "Google";       // selected provider name 
  		// setup a new connection// setup a new connection
  
  		$provider_adapter = $this->hybridauth->setup( $provider, $params );

  		// catch if setup error
  
  		if( ! $provider_adapter ) return false;    
 
   		$login_is_ok = $provider_adapter->login();
  		// catch if login error
  
  		if( ! $login_is_ok ) return false;
  		
  		return true;  		

	}
	
	function loggedin()
	{
		$hasSession = $this->hybridauth->hasSession();
		$fisrtTime = $this->user->first_time();
		return $hasSession && !$fisrtTime;
	}
	
	function hasSession()
	{
		return $this->hybridauth->hasSession();
	}
	
	function provider_profile()
	{
		return $this->hybridauth->wakeup();
	}
	
	function first_data()
	{
		$provider_adapter = $this->hybridauth->wakeup();
		$userData["firstname"] = $provider_adapter->user()->profile->firstName;
		$userData["lastname"] = $provider_adapter->user()->profile->lastName;
		$userData["email"] = $provider_adapter->user()->profile->email;
		return $userData;
	}
	
	function logout()
	{
      	// if user is connected 
      	if( $this->hybridauth->hasSession() ){
      		$provider_adapter = $this->hybridauth->wakeup(); 
      		$provider_adapter->logout();
      	}
      	// then 
      	$this->hybridauth->expireStorage(); 
      	return true;
	}
	
	function first_time()
	{
		if($this->hasSession()){
			$provider_adapter = $this->provider_profile();
			$email = $provider_adapter->user()->profile->email;
			$query = $this->db->get_where('users', array('email' => $email));
			return $query->num_rows() == 0;
		}else
			return false;
		
	}
	
	function exist_id($id){
		$query = $this->db->get_where('users', array('id' => $id));
		return $query->num_rows() > 0;
	}
	
	function exist_email($email){
		$query = $this->db->get_where('users', array('email' => $email));
		return $query->num_rows() > 0;
	}
	
	function get(){
		$provider_adapter = $this->provider_profile();
		$email = $provider_adapter->user()->profile->email;
		
		$query = $this->db->get_where('users', array('email' => $email));
		if($query->num_rows() == 1)
			return $query->row_array();
		else
			return false;
	}
	
	function is_admin(){
		$provider_adapter = $this->provider_profile();
		$email = $provider_adapter->user()->profile->email;
		
		$query = $this->db->get_where('users', array('email' => $email));
		if($query->num_rows() == 1)
			return $query->first_row()->is_admin == 1;
		else
			return false;
	}
	
	function get_id($id){
		$query = $this->db->get_where('users', array('id' => $id));
		if($query->num_rows() == 1)
			return $query->row_array();
		else
			return false;
	}
	
	function is_association_admin($associationID)
	{
		$provider_adapter = $this->provider_profile();
		$email = $provider_adapter->user()->profile->email;
		$query = $this->db->get_where('users', array('email' => $email));
		if($query->num_rows() == 1){
			$this->load->model('Associations_model', 'association');
			
			$user =  $query->row_array();
			$this->db->select('role');
			$this->db->from('users_associations');
			$this->db->join('users', 'users.id = users_associations.user_id');
			$this->db->where('user_id', $user["id"]); 
			$this->db->where('association_id', $associationID);
			$this->db->where('valid', 1);  
			$query = $this->db->get();
			$result = array();
			foreach ($query->result() as $row)
			{
				if($row->role == "admin")
					return true;
			}
			return false;
			
		}else
			return false;
	}
	
	function get_associations(){
		$provider_adapter = $this->provider_profile();
		$email = $provider_adapter->user()->profile->email;
		$query = $this->db->get_where('users', array('email' => $email));
		if($query->num_rows() == 1){
			$this->load->model('Associations_model', 'association');
			
			$user =  $query->row_array();
			$this->db->select('association_id');
			$this->db->from('users_associations');
			$this->db->join('users', 'users.id = users_associations.user_id');
			$this->db->where('user_id', $user["id"]);
			$this->db->where('valid', 1); 
			$query = $this->db->get();
			$result = array();
			foreach ($query->result() as $row)
			{
				$association	= $this->association->get_id($row->association_id);
				$association["is_admin"] = $this->is_association_admin($row->association_id);
			   	$result[]		=  $association;
			}
			return $result;
			
		}else
			return false;
		
	}
	

}

/*
			$userData["photoURL"] = $provider_adapter->user()->providerId;
			$userData["providerUID"] = $provider_adapter->user()->providerUID;
			$userData["timestamp"] = $provider_adapter->user()->timestamp;
			$userData["profileURL"] = $provider_adapter->user()->profile->profileURL;
			$userData["webSiteURL"] = $provider_adapter->user()->profile->webSiteURL;
			$userData["photoURL"] = $provider_adapter->user()->profile->photoURL;
			$userData["displayName"] = $provider_adapter->user()->profile->displayName;
			$userData["displayName"] = $provider_adapter->user()->profile->description;
			$userData["firstName"] = $provider_adapter->user()->profile->firstName;
			$userData["lastName"] = $provider_adapter->user()->profile->lastName;
			$userData["lastName"] = $provider_adapter->user()->profile->gender;
			$userData["language"] = $provider_adapter->user()->profile->language;
			$userData["age"] = $provider_adapter->user()->profile->age;
			$userData["birthDay"] = $provider_adapter->user()->profile->birthDay;
			$userData["birthMonth"] = $provider_adapter->user()->profile->birthMonth;
			$userData["birthYear"] = $provider_adapter->user()->profile->birthYear;
			$userData["email"] = $provider_adapter->user()->profile->email;
			$userData["phone"] = $provider_adapter->user()->profile->phone;
			$userData["address"] = $provider_adapter->user()->profile->address;
			$userData["country"] = $provider_adapter->user()->profile->country;
			$userData["region"] = $provider_adapter->user()->profile->region;
			$userData["city"] = $provider_adapter->user()->profile->city;
			$userData["zip"] = $provider_adapter->user()->profile->zip;

*/